package com.loadmore.common

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import com.blankj.utilcode.util.CrashUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.loadmore.common.dao.gen.DaoMaster
import com.loadmore.common.dao.gen.DaoSession
import com.lzy.okgo.OkGo
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import java.io.File
import okhttp3.OkHttpClient
import com.lzy.okgo.interceptor.HttpLoggingInterceptor
import skin.support.SkinCompatManager
import skin.support.app.SkinAppCompatViewInflater
import skin.support.app.SkinCardViewInflater
import skin.support.constraint.app.SkinConstraintViewInflater
import skin.support.design.app.SkinMaterialViewInflater
import skin.support.flycotablayout.SkinFlycoTabLayoutManager
import skin.support.flycotablayout.app.SkinFlycoTabLayoutInflater
import solid.ren.skinlibrary.SkinConfig
import solid.ren.skinlibrary.base.SkinBaseApplication
import java.util.logging.Level




/**
 *@author sunleilei
 *@date 2018/7/12
 *@time 7:59
 *
 */
open class App : SkinBaseApplication() {



    companion object {

        lateinit var app:App
        lateinit var daoSession: DaoSession
        fun setDataBase(context: Context, userid:Long) {
            var db = DaoMaster.DevOpenHelper(context,"$userid").writableDatabase
            var master = DaoMaster(db)
            daoSession = master.newSession()
        }
    }
    override fun onCreate() {
        super.onCreate()
        app = this
        init()
    }



    open fun init() {
        initNetRequest()
        initRefreshLayout()
        initLog()
        initUtils()
        initIcons()
        initSkin()
    }

    private fun initSkin() {

        SkinCompatManager.withoutActivity(this)
                .addInflater(SkinAppCompatViewInflater())           // 基础控件换肤初始化
                .addInflater(SkinMaterialViewInflater())            // material design 控件换肤初始化[可选]
                .addInflater(SkinConstraintViewInflater())          // ConstraintLayout 控件换肤初始化[可选]
                .addInflater(SkinCardViewInflater())                // CardView v7 控件换肤初始化[可选]
                .addInflater(SkinFlycoTabLayoutInflater())
                .loadSkin()
    }

    private fun initIcons() {

    }

    private fun initUtils() {
        Utils.init(this)
        CrashUtils.init()
        val config:LogUtils.Config = LogUtils.getConfig()
        val dir = File(externalCacheDir,"log")
        if (!dir.exists()) {
            dir.mkdirs()
        }
        config.setDir(dir).setGlobalTag("oneday")

    }
    private fun initLog() {


    }

    private fun initRefreshLayout() {
        //设置全局的Header构建器
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white)//全局设置主题颜色
            ClassicsHeader(context)//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            //指定为经典Footer，默认是 BallPulseFooter
            ClassicsFooter(context).setDrawableSize(20f)
        }
    }


    private fun initNetRequest() {
        val builder = OkHttpClient.Builder()
        val loggingInterceptor = HttpLoggingInterceptor("oneday")
        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.NONE)
        //log颜色级别，决定了log在控制台显示的颜色
        //loggingInterceptor.setColorLevel(Level.INFO)
        builder.addInterceptor(loggingInterceptor)
        OkGo.getInstance().init(this).okHttpClient = builder.build()
    }

    fun isMainProcess(): Boolean {
        val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val processInfos = am.runningAppProcesses
        val mainProcessName = packageName
        val myPid = android.os.Process.myPid()
        for (info in processInfos) {
            if (info.pid == myPid && mainProcessName == info.processName) {
                return true
            }
        }
        return false
    }

}